var common = require("../../utils/Conmon.js");
var QQMapWX = require("../../utils/qqmap-wx-jssdk.min.js");
var globalData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: {},
    ObjectID: 0,
    ObjectType: 1,
    CustID: 0
  },
  location: function () {
    var that = this;
    var qqmapsdk = new QQMapWX({
      key: 'XT5BZ-X5YHX-F4F4F-ZFF3E-5EMGS-TXF5U'
    });
    wx.getLocation({
      type:"gcj02",
      success: function (res) {
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (r) {
         
            var info = r.result;
            var address = info.address_component.province + info.address_component.city+info.formatted_addresses.recommend;
           
            var userid = wx.getStorageSync('UserInfo').UserID;
            if (userid>0)
            {
              wx.request({
                url: globalData.apiurl + '/member/api/Register/UpdateAddress',
                data: { userId: userid, addr: address, custId: globalData.custID},
                method: 'get',
                header: {
                  'content-type': 'application/json',
                  'X_ManuName': globalData.manuName
                },
                success: function (res) {

                },
                fail: function (res){


                }
              })
            }
           
         
          },
          fail: function (res) {
         
          },
          complete: function (res) {
           
          }
        });

      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
    */ 
  onLoad: function(options) {
    console.log(options.code_ticket);
    var ObjectType = 1;
    var ObjectID = 0;
    if (options.ObjectType > 0) {
      ObjectType = options.ObjectType;
    }
    if (options.ObjectID > 0 && ObjectType > 1) {
      ObjectID = options.ObjectID;
    }
   
    var custId = globalData.custID;

    this.setData({
      CustID: globalData.custID,
      ObjectID: ObjectID,
      ObjectType: ObjectType
    });
    wx.showLoading({
     title: '加载中...',
      mask: true
    });
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
   
    this.dialog = this.selectComponent("#dialog");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
    common.pageInit(function() {});
   
    var _this = this;
    var app = getApp();
    app.initGlobalData(function () {

      var ObjectType = _this.data.ObjectType;
      var ObjectID = _this.data.ObjectID;
     
      var globalData = app.globalData;
      var custId = globalData.custID;

 
      var postData = {
        custID: globalData.custID,
        Platform: "WxAPP",
        ObjectID: ObjectID,
        ObjectType: ObjectType,
        type: "Home"
      };
      wx.request({
        url: globalData.apiurl + '/magic/api/MagicMall/DefaultJson',
        data: JSON.stringify(postData),
        method: 'post',
        header: {
          'content-type': 'application/json',
          'X_ManuName': globalData.manuName
        },
        success: function (res) {
          wx.hideLoading();
          var page = JSON.parse(res.data.content);
          _this.setData({
            page: page
          });
          wx.setNavigationBarTitle({
            title: page.Title,
          })
          // console.log(page);
          _this.location();
        }
      })
    });


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },



  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})